package ru.ifmo.ctddev.krotkov.taskrunner;

import java.util.ArrayDeque;

public class Worker implements Runnable {

	ArrayDeque<MyTask<?, ?>> q;

	@Override
	public void run() {
		while (true) {
			MyTask<?, ?> z = null;
			try {
				synchronized (q) {
					while (q.isEmpty()) {
						q.wait();
					}

					z = q.poll();
				}
				z.execute();
				
				synchronized (z) {
					z.notify();
				}

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	Worker(ArrayDeque<MyTask<?, ?>> q) {
		this.q = q;
	}
}
